package com.sanjiahantang.youjiaclient.module.common.presenters;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.module.common.beans.HelpBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiaclient.module.common.views.impl.WebActivity;

import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_ID;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_KEYWORDS;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TYPE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;
import static com.sanjiahantang.youjiaclient.config.StaticValue.WEB_TYPE_HELP;

/**
 * Created by zy on 2017/10/16.
 */

public class SearchPresenter extends BaseMvpPresenter<BaseView> {

    private IUserModel userModel;
    private QuickAdapter<HelpBean.Item> helpAdapter;

    public SearchPresenter(){
        userModel = new UserModel();
    }

    public void initAdapter(Context context,AbsListView listView){
        if(helpAdapter == null){
            helpAdapter = new QuickAdapter<HelpBean.Item>(context, R.layout.item_text) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final HelpBean.Item item) {
                    helper.setText(R.id.text1,item.getArTitle());
                    /*ImageView ivIcon = helper.getView(R.id.ivIcon);
                    if (!TextUtils.isEmpty(item.getUrl())){
                        Picasso.with(context).load(ConfigValue.IMG_IP + item.getUrl()).into(ivIcon);
                    }

                    try {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
                        Date date = df.parse(item.getCreateTime());
                        helper.setText(R.id.txtTime, RelativeDateFormat.format(date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }*/
                    helper.setVisible(R.id.ivArcher,true);
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, WebActivity.class);
                            intent.putExtra(KEY_ID,item.getArticleNo());
                            intent.putExtra(KEY_TYPE,WEB_TYPE_HELP);
                            context.startActivity(intent);
                        }
                    });
                }
            };
        }
        listView.setAdapter(helpAdapter);
    }
    public void loadHelpList(Context context, String keyWords) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,String> map = new HashMap<>();
        map.put("arTitle",keyWords);
        param.put(KEY_PARAM,map);
        mView.showProgress();
        userModel.loadHelpList(context,param,new LoadHelpListCallBack());
    }

    private class LoadHelpListCallBack implements CallBack<HelpBean>{


        @Override
        public void onSuccess(HelpBean response, Context context, Object tag) {
            hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC && response.getData() != null && response.getData().getList() != null){
                helpAdapter.clear();
                helpAdapter.addAll(response.getData().getList());
            }

        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }

    private void hideProgress() {
        if (mView != null){
            mView.hideProgress();
        }
    }
}
